﻿using System;
using System.Diagnostics;
using System.IO;
using System.Windows;

namespace UDPshutdown1.utils
{


    public static class AppAutoStarter
    {
        // 获取自动启动快捷方式的文件路径
        public static string LnkFilePath
        {
            get
            {
                return Path.Combine(
                    Environment.GetFolderPath(Environment.SpecialFolder.Startup),
                    $"{AppDomain.CurrentDomain.FriendlyName}.lnk");
            }
        }

        // 禁用自动启动
        public static void DisableAutoStart()
        {
            if (File.Exists(LnkFilePath))
            {
                File.Delete(LnkFilePath);
            }
        }

        // 启用自动启动
        public static void EnableAutoStart()
        {
            if (File.Exists(LnkFilePath))
            {
                return;
            }

            var shellType = Type.GetTypeFromProgID("WScript.Shell");
            dynamic shell = Activator.CreateInstance(shellType);
            var shortcut = shell.CreateShortcut(LnkFilePath);

            //获取应用 直链
            string appStartupPath = Process.GetCurrentProcess().MainModule.FileName;
            shortcut.TargetPath = appStartupPath;

            //获取应用所在目录
            shortcut.WorkingDirectory = AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
            shortcut.Save();
        }
    }
}
